package com.dkd.easybook.tools;
import com.dkd.common.enums.UpdateType;
import com.dkd.common.utils.SecurityUtils;
import com.dkd.common.utils.spring.SpringUtils;
import com.dkd.easybook.domain.EbAttachmentRelations;
import com.dkd.easybook.domain.EbAttachments;
import com.dkd.easybook.service.impl.EbAttachmentRelationsServiceImpl;
import com.dkd.easybook.service.impl.EbAttachmentsServiceImpl;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
@Slf4j
public class EbTools {
    public static void handleIsertAttachmentsRelations(String tableName, Long recordId, List<Long> attachmentIds) {
        EbAttachmentRelationsServiceImpl ebAttachmentRelationsServiceImpl = SpringUtils.getBean(EbAttachmentRelationsServiceImpl.class);
        // 查询附件关系表是否有数据，有则删除
        EbAttachmentRelations attachmentRelations = new EbAttachmentRelations();
        attachmentRelations.setTableName(tableName);
        attachmentRelations.setRecordId(recordId);
        List<EbAttachmentRelations> ebAttachmentRelationsList = ebAttachmentRelationsServiceImpl.selectEbAttachmentRelationsList(attachmentRelations);
        if (ebAttachmentRelationsList.size() > 0) {
            Integer deleteResult = ebAttachmentRelationsServiceImpl.deleteEbAttachmentRelationsByIds(ebAttachmentRelationsList.stream()
                    .map(EbAttachmentRelations::getId)
                    .toArray(Long[]::new));
            log.info("删除附件关系表数据：" + deleteResult);
        }
        // 插入附件关系表
        if (attachmentIds != null && attachmentIds.size() > 0) {
            for (Long attachmentId : attachmentIds) {
                EbAttachmentRelations ebAttachmentRelations = new EbAttachmentRelations();
                ebAttachmentRelations.setAttachmentId(attachmentId);
                ebAttachmentRelations.setTableName(tableName);
                ebAttachmentRelations.setRecordId(recordId);
                ebAttachmentRelationsServiceImpl.insertEbAttachmentRelations(ebAttachmentRelations);
            }
        }
    }
    static public List<EbAttachments> getAttachmentsByRecordId(String tableName, Long recordId) {
        EbAttachmentRelationsServiceImpl ebAttachmentRelationsServiceImpl = SpringUtils.getBean(EbAttachmentRelationsServiceImpl.class);
        EbAttachmentRelations ebAttachmentRelations = new EbAttachmentRelations();
        EbAttachmentsServiceImpl ebAttachmentsServiceImpl = SpringUtils.getBean(EbAttachmentsServiceImpl.class);
        ebAttachmentRelations.setTableName(tableName);
        ebAttachmentRelations.setRecordId(recordId);
        List<EbAttachmentRelations> ebAttachmentRelationsList = ebAttachmentRelationsServiceImpl.selectEbAttachmentRelationsList(ebAttachmentRelations);
        List<EbAttachments> ebAttachmentsListEmpty = new ArrayList<>();
        if (ebAttachmentRelationsList.size() > 0) {
            List<Long> attachmentIds = ebAttachmentRelationsList.stream()
                    .map(EbAttachmentRelations::getAttachmentId)
                    .collect(Collectors.toList());
            List<EbAttachments> ebAttachmentsList = ebAttachmentsServiceImpl.listByIds(attachmentIds);
            ebAttachmentsList.stream()
                    .forEach(ebAttachments -> {
                        ebAttachmentsListEmpty.add(ebAttachments);
                    });
        }
        return ebAttachmentsListEmpty;
    }
}
